﻿using aier.dataaccess.model;
using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.business.logic
{
    public class IdentityCardOcrBll
    {
        public ILog log { get; set; }

        public ResResult resResult { get; set; }

        public string accessKeyId { get; set; }

        public string accessSecret { get; set; }

        public string endpoint { get; set; }

        /// <summary>
        /// 识别身份证图片上信息
        /// </summary>
        /// <param name="imgPath">图片路径</param>
        /// <param name="side">face</param>
        /// <returns></returns>
        public async Task<ResResult> GetIdentityCardByImg(string imgPath, string side= "face")
        {
            try
            {
                AlibabaCloud.SDK.VIAPI.Utils.FileUtils fileobj = AlibabaCloud.SDK.VIAPI.Utils.FileUtils.getInstance(this.accessKeyId, this.accessSecret);
                string imgResult = fileobj.Upload(imgPath);


                AlibabaCloud.SDK.Ocr20191230.Client client = CreateClient();

                AlibabaCloud.SDK.Ocr20191230.Models.RecognizeIdentityCardRequest recognizeIdentityCardRequest
                    = new AlibabaCloud.SDK.Ocr20191230.Models.RecognizeIdentityCardRequest
                    {
                        ImageURL = imgResult,
                        Side = side,
                    };
                // 复制代码运行请自行打印 API 的返回值
                var orcresult = await client.RecognizeIdentityCardAsync(recognizeIdentityCardRequest);
                var cardidentity = orcresult.Body.Data.FrontResult;
                IdCardMolde idCardMolde = new IdCardMolde()
                {
                    Name = cardidentity.Name,
                    Nationality = cardidentity.Nationality,
                    IDNumber = cardidentity.IDNumber,
                    Address = cardidentity.Address,
                    Gender = cardidentity.Gender,
                    BirthDate = cardidentity.BirthDate
                };

                return resResult.SuccessResult(idCardMolde);
            }
            catch (Exception ex) {
                log.Error(ex.StackTrace, ex);
                return resResult.FailResult(ex.Message);
            }
            
        }


        /// <summary>
        /// 识别身份证图片上信息
        /// </summary>
        /// <param name="fileName">图片名</param>
        /// <param name="fileStream">图片流</param>
        /// <param name="side">face</param>
        /// <returns></returns>
        public async Task<ResResult> GetIdentityCardByImg(string fileName, Stream fileStream, string side = "face")
        {
            try
            {
                AlibabaCloud.SDK.VIAPI.Utils.FileUtils fileobj = AlibabaCloud.SDK.VIAPI.Utils.FileUtils.getInstance(this.accessKeyId, this.accessSecret);
                string imgResult = fileobj.Upload(fileName, fileStream);


                AlibabaCloud.SDK.Ocr20191230.Client client = CreateClient();

                AlibabaCloud.SDK.Ocr20191230.Models.RecognizeIdentityCardRequest recognizeIdentityCardRequest
                    = new AlibabaCloud.SDK.Ocr20191230.Models.RecognizeIdentityCardRequest
                    {
                        ImageURL = imgResult,
                        Side = side,
                    };
                // 复制代码运行请自行打印 API 的返回值
                var orcresult = await client.RecognizeIdentityCardAsync(recognizeIdentityCardRequest);
                var cardidentity = orcresult.Body.Data.FrontResult;
                IdCardMolde idCardMolde = new IdCardMolde()
                {
                    Name = cardidentity.Name,
                    Nationality = cardidentity.Nationality,
                    IDNumber = cardidentity.IDNumber,
                    Address = cardidentity.Address,
                    Gender = cardidentity.Gender,
                    BirthDate = cardidentity.BirthDate
                };

                return resResult.SuccessResult(idCardMolde);
            }
            catch (Tea.TeaException ex)
            {
                log.Error(ex.StackTrace, ex);
                return resResult.FailResult(string.Concat(ex.Message, ex.Code));
            }
            catch (Exception ex)
            {

                log.Error(ex.StackTrace, ex);
                return resResult.FailResult(string.Concat(ex.Message, ((Tea.TeaException)ex)?.Code));
            }
        }

        /**
         * 使用AK&SK初始化账号Client
         * @param accessKeyId
         * @param accessKeySecret
         * @return Client
         * @throws Exception
         */
        private  AlibabaCloud.SDK.Ocr20191230.Client CreateClient()
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 您的AccessKey ID
                AccessKeyId = this.accessKeyId,
                // 您的AccessKey Secret
                AccessKeySecret = this.accessSecret,
            };
            // 访问的域名
            config.Endpoint = this.endpoint; //"ocr.cn-shanghai.aliyuncs.com";
            return new AlibabaCloud.SDK.Ocr20191230.Client(config);
        }
    }
}
